package com.cw.base.common.dispatch.task.facade.ref;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import com.cw.base.common.dispatch.obtain.TaskService;
import com.cw.base.common.dispatch.pojo.Task;
import com.cw.base.common.dispatch.task.facade.TaskFacade;
import com.cw.base.common.dispatch.task.facade.pojo.request.MethodTaskRequest;
import com.cw.base.common.dispatch.task.facade.pojo.request.TaskRequest;
import com.cw.base.common.dispatch.task.facade.ref.converter.TaskRequestConverter;
import com.cw.starter.center.dto.CommonResultDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * 补偿任务的接口<br>
 * Created on 15/11/9.
 *
 * @author zhangcheng
 */
public class CompensateTaskFacade extends AbstractCompensateTaskFacade<TaskRequest> implements TaskFacade {

    @Autowired
    @Qualifier("obtainTask")
    private TaskService taskService;

    @Override
    public CommonResultDto receiveTask(@NotNull(message = "request 是空的！") @Valid TaskRequest taskRequest) {
        return facadeManger(taskRequest);
    }

    @Override
    public CommonResultDto receiveTask(MethodTaskRequest methodTaskRequest) {
        TaskRequest taskRequest = new TaskRequest();
        taskRequest.setRequest(methodTaskRequest.getRequest());
        taskRequest.setBaseUserReqDto(methodTaskRequest.getBaseUserReqDto());
        taskRequest.setTargetMethod(methodTaskRequest.getTargetMethod());
        taskRequest.setParameters(methodTaskRequest.getParameters());
        taskRequest.setTargetClass(methodTaskRequest.getTargetClass());
        taskRequest.setDelayExecTime(methodTaskRequest.getDelayExecTime());
        taskRequest.setApartExecTime(methodTaskRequest.getApartExecTime());
        taskRequest.setLoopCondition(methodTaskRequest.getLoopCondition());
        taskRequest.setExecNum(methodTaskRequest.getExecNum());
        taskRequest.setErrorLoop(methodTaskRequest.isErrorLoop());
        taskRequest.setTaskCallBackClass(methodTaskRequest.getTaskCallBackClass());
        taskRequest.setTaskCallBackMethod(methodTaskRequest.getTaskCallBackMethod());
        taskRequest.setErrorTaskCallBackClass(methodTaskRequest.getErrorTaskCallBackClass());
        taskRequest.setErrorTaskCallBackMethod(methodTaskRequest.getErrorTaskCallBackMethod());
        taskRequest.setBusinessParam(methodTaskRequest.getBusinessParam());
        return facadeManger(taskRequest);
    }

    @Override
    protected CommonResultDto normal(TaskRequest taskRequest) {
        Task task = TaskRequestConverter.convert(taskRequest);
        taskService.receiveTask(task);
        // 成功
        return CommonResultDto.success();
    }
}
